package com.iwop.moto.controller;

import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.model.User;
import com.iwop.moto.service.AnnexService;
import com.iwop.moto.service.FOrderServiceService;
import com.iwop.moto.service.UserService;
import com.iwop.moto.utils.PushNoticeUtil;
import com.iwop.moto.utils.stringUtil.ObjectId;
import com.iwop.moto.utils.stringUtil.SequenceUtil;
import com.iwop.moto.utils.stringUtil.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/9/1.
 */
@Controller
public class FOrderServiceController extends  BaseController {
    @Autowired
    FOrderServiceService fOrderServiceService;
    @Autowired
    UserService userService;
    @Autowired
    AnnexService annexService;
    /**
     * 测试地址：192.168.1.158:8888/api/store_order/add?store_user_id=1&user_id=2
     * 功能：添加推送订单<br>
     * 参数：user_id 用户id<br>
     * 参数：username 用户名称<br>
     * 参数：user_tel 用户电话<br>
     * 参数：sid 主键<br>
     * 参数：orderno 订单id<br>
     * 参数：status_time 修改时间<br>
     * 参数：status_flag 标识<br>
     * 参数：store_id 店铺id<br>
     * @return
     *
     * */

    @RequestMapping("/api/store_order/add")
    @ResponseBody
    public ResultModel insertStoreOrder(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            if(SRModel.containsKey("user_id")){
                User userBySid = userService.getUserBySid(SRModel.getString("user_id"));
                SRModel.put("user_name",userBySid.getNickname());
                SRModel.put("user_tel",userBySid.getTel());
            }
            String sid = ObjectId.userUUID();
            SRModel.put("sid", sid);
            SRModel.put("orderno", SequenceUtil.Serial("SO"));
            SRModel.put("pushstatus","0");
                SRModel.put("order_type", "store");
            SRModel.put("addtime",new Date());
            SRModel.put("status_flag","1");
            SRModel.put("status_time",new Date());
            fOrderServiceService.insertStoreOrder(SRModel);
            return getSucceed(SRModel,ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 用户推送付款
     * */
    public ResultModel updateStoreOrderByUser(){
        SuperRecordModel SRModel = this.getSRModel();
        SRModel.put("status_time",new Date());
        try {
            SuperRecordModel storeOrder = fOrderServiceService.getStoreOrder(SRModel);
            SRModel.put("sid", storeOrder.getString("sid"));
            fOrderServiceService.updateStoreOrder(SRModel);
            return getSucceed(ErrCode.SUCCEED);
        }catch (Exception e){
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 功能：修改推送付款
     * 参数：user_name 用户昵称
     * 参数：user_tel 用户号码
     * 参数：content 维修内容
     * 参数：money 维修金额
     * 参数：orderno 订单编号
     * 参数：mStoreGoods：[{endtime:'',starttim3e:'',pictrue:'',name:''}]
     * */
    @RequestMapping("/api/store_order/edit")
    @ResponseBody
    public ResultModel upStoreOrder(){
        SuperRecordModel SRModel = this.getSRModel();
        SRModel.put("status_time",new Date());
        try {
            SuperRecordModel storeOrder = fOrderServiceService.getStoreOrder(SRModel);
            if(storeOrder==null){
                return getErr(ErrCode.STORE_ERROR_SERVER_NOEXIST);
            }
            if(storeOrder.containsKey("money")){
                BigDecimal bigDecimal = (BigDecimal) storeOrder.get("money");
                if(bigDecimal!=null){
                    double rmoney = bigDecimal.doubleValue();
                    if (rmoney>0){
                        return getErr(ErrCode.ERROR_ORDER_SERVICE_SUBMIT);
                    }
                }
            }
            if(StringUtils.isEmpty(storeOrder.getString("user_id"))){
                String user_tel = SRModel.getString("user_tel");
                User user = new User();
                user.setTel(user_tel);
                user.setUserType("popular");
                user = userService.login(user);
                if(user!=null){
                    SRModel.put("user_id",user.getSid());
                }else {
                    return getErr(ErrCode.USER_ERROR_NOEXIST);
                }
                SRModel.put("user_name",user.getNickname());
            }else {
                SRModel.put("user_id",storeOrder.getString("user_id"));
            }
            SRModel.put("sid",storeOrder.getString("sid"));
            fOrderServiceService.updateStoreOrder(SRModel);
            fOrderServiceService.deleteStoreOrderDetail(SRModel.getString("sid"));
            String mStoreGoods = SRModel.getString("mStoreGoods");
            if(!StringUtils.isEmpty(mStoreGoods)){
                List<SuperRecordModel> lsDetail = SRModel.jsonToList(mStoreGoods);
                if(lsDetail!=null && lsDetail.size()>0){
                    for (SuperRecordModel srmDetail : lsDetail){
                        srmDetail.put("sid",UUID.randomUUID().toString());
                        srmDetail.put("store_good_id",storeOrder.getString("sid"));
                        srmDetail.put("status","0");
                        fOrderServiceService.insertStoreGoods(srmDetail);
                    }
                }
            }
            PushNoticeUtil.sendPersonNotice("您有一条新的付款消息","user",SRModel.getString("user_id"));
            return getSucceed("yes",ErrCode.SUCCEED);
        } catch (IOException e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 功能：获取推送付款
     * 参数：orderno 订单id
     * */
    @RequestMapping("/api/store_order/get")
    @ResponseBody
    public ResultModel getStoreOrder(){
        SuperRecordModel SRModel = this.getSRModel();
        SuperRecordModel storeOrder = null;
        try {
            storeOrder = fOrderServiceService.getStoreOrder(SRModel);
            List<SuperRecordModel> list= fOrderServiceService.getStoreOrderDetail(storeOrder.getString("sid"));
            storeOrder.put("orderGoods",list);
            return getSucceed(storeOrder,ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 获取售后保证没过期的商品
     * 参数：user_id 用户id
     * 参数：out_status 查询是否过期的数据 0过期，1不过期
     * */
    @RequestMapping("/api/store_order/detail/get")
    @ResponseBody
    public ResultModel getStoreOrderdetail(){
        SuperRecordModel SRModel = this.getSRModel();
        List<SuperRecordModel> storeOrderDetailList = null;
        try {
            storeOrderDetailList = fOrderServiceService.getStoreOrderDetailList(SRModel);
            return getSucceed(storeOrderDetailList,ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    @RequestMapping("/api/store_order/list")
    @ResponseBody
    public ResultModel getStoreOrderList(){
        SuperRecordModel SRModel = this.getSRModel();
        SRModel.put("order_type","store");
        List<SuperRecordModel> storeOrderDetailList = null;
        try {
            storeOrderDetailList = fOrderServiceService.getStoreOrderList(SRModel);
            return getSucceed(storeOrderDetailList,ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 订单列表
     * user_id用户id
     * */
    @RequestMapping("/api/store_order/user/list")
    @ResponseBody
    public ResultModel getStoreServiceForUserList(){
        SuperRecordModel SRModel = this.getSRModel();
        List<SuperRecordModel> storeOrderDetailList = null;
        try {
            storeOrderDetailList = fOrderServiceService.getStoreServiceForUserList(SRModel);
            return getSucceed(storeOrderDetailList,ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

}
